package com.have.blog.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.SystemDictTypeDto;
import com.have.blog.admin.service.SystemDictTypeService;
import com.have.blog.admin.utils.SecurityUtil;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.SysDictDataMapper;
import com.have.blog.model.mapper.SysDictTypeMapper;
import com.have.blog.model.models.SysDictDataExample;
import com.have.blog.model.models.SysDictType;
import com.have.blog.model.models.SysDictTypeExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@SuppressWarnings("all")
public class SystemDictTypeServiceImpl implements SystemDictTypeService {
    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public Wrapper<CommonPage<SystemDictTypeDto>> getPageList(SystemDictTypeDto systemDictTypeDto) {
        Page page = PageHelper.startPage(systemDictTypeDto.getPageNum(), systemDictTypeDto.getPageSize());
        SysDictTypeExample example = new SysDictTypeExample();
        SysDictTypeExample.Criteria criteria = example.createCriteria();
        // 字典名称
        if (StringUtils.isNotEmpty(systemDictTypeDto.getDictName()) && StringUtils.isNotEmpty(systemDictTypeDto.getDictName().trim())) {
            criteria.andDictNameLike("%" + systemDictTypeDto.getDictName() + "%");
        }
//         字典类型
        if (StringUtils.isNotEmpty(systemDictTypeDto.getDictType()) && StringUtils.isNotEmpty(systemDictTypeDto.getDictType().trim())) {
            criteria.andDictTypeLike("%" + systemDictTypeDto.getDictType().trim() + "%");
        }
        if (StringUtils.isNotEmpty(systemDictTypeDto.getOrderByAscColumn())) {
            String column = StringUtils.underLine(new StringBuffer(systemDictTypeDto.getOrderByAscColumn())).toString();
            example.setOrderByClause(column + " asc");
        } else if (StringUtils.isNotEmpty(systemDictTypeDto.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(systemDictTypeDto.getOrderByDescColumn())).toString();
            example.setOrderByClause(column + " desc");
        } else {
            example.setOrderByClause("sort,create_time desc");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<SysDictType> sysDictTypeList = dictTypeMapper.selectByExample(example);
        List<SystemDictTypeDto> resultList = MyBeanUtils.copyListProperties(sysDictTypeList, SystemDictTypeDto::new);
        CommonPage<SystemDictTypeDto> commonPage = CommonPage.restPage(resultList);
        commonPage.setTotal(page.getTotal());
        commonPage.setPageNum(page.getPageNum());
        commonPage.setPageSize(page.getPageSize());
        return Wrapper.success(commonPage);
    }

    @Override
    public Wrapper addSysDictType(SystemDictTypeDto systemDictTypeDto) {
        Integer adminUid = SecurityUtil.getUserId();
        SysDictTypeExample example = new SysDictTypeExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andDictTypeEqualTo(systemDictTypeDto.getDictType());
        long count = dictTypeMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        SysDictType dictType = new SysDictType();
        MyBeanUtils.copyProperties(systemDictTypeDto, dictType);
        dictType.setCreatedAdminUid(adminUid);
        dictType.setUpdatedAdminUid(adminUid);
        dictType.setStatus(BlogStatus.ENABLE.getStatus());
        int result = dictTypeMapper.insert(dictType);
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editSysDictType(SystemDictTypeDto systemDictTypeDto) {
        Integer adminUid = SecurityUtil.getUserId();
        SysDictType sysDictType = dictTypeMapper.selectByPrimaryKey(systemDictTypeDto.getUid());
        if (!sysDictType.getDictType().equals(systemDictTypeDto.getDictType())) {
            SysDictTypeExample example = new SysDictTypeExample();
            example.createCriteria()
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                    .andDictTypeEqualTo(systemDictTypeDto.getDictType());
            long count = dictTypeMapper.countByExample(example);
            if (count > 0) {
                return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
            }
        }
        sysDictType.setDictType(systemDictTypeDto.getDictType());
        sysDictType.setDictName(systemDictTypeDto.getDictName());
        sysDictType.setRemark(systemDictTypeDto.getRemark());
        sysDictType.setIsPublish(systemDictTypeDto.getIsPublish());
        sysDictType.setSort(systemDictTypeDto.getSort());
        sysDictType.setUpdatedAdminUid(adminUid);
        int result = dictTypeMapper.updateByPrimaryKeySelective(sysDictType);
        // TODO redis相关操作
        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchSysDictType(List<Integer> uids) {
        Integer adminUid = SecurityUtil.getUserId();

        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.failed(AdminMessageConstant.PARAM_INCORRECT);
        }

        // 判断要删除的分类，是否有博客
        SysDictDataExample example = new SysDictDataExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andDictTypeUidIn(uids);

        long count = dictDataMapper.countByExample(example);
        if (count > 0) {
            return Wrapper.failed(AdminMessageConstant.DICT_DATA_UNDER_THIS_SORT);
        }

        SysDictTypeExample typeExample = new SysDictTypeExample();
        typeExample.createCriteria()
                .andUidIn(uids)
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        SysDictType template = new SysDictType();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        int result = dictTypeMapper.updateByExampleSelective(template, typeExample);

        if (result > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        } else {
            return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
        }
    }

    public List<SystemDictTypeDto> getAll() {
        SysDictTypeExample example = new SysDictTypeExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        List<SysDictType> sysDictTypes = dictTypeMapper.selectByExample(example);
        List<SystemDictTypeDto> systemDictTypeDtos = MyBeanUtils.copyListProperties(sysDictTypes, SystemDictTypeDto::new);
        return systemDictTypeDtos;
    }
}
